<?php

$mysql_host = "localhost";
$mysql_database = "group09_zxq_db";
$mysql_user = "326270_group09";
$mysql_password = "APMproject1";

//TODO: checks and error handling in the most important places;

$phone_id = intval($_GET['phone_id']);
//will need to implement something like mysql_real_escape_string() for all GET attributes that might not be numbers;
$latitude = $_GET['latitude'];
$longitude = $_GET['longitude'];
$time = $_GET['time'];
//time format must be: yyyymmddHHMMSS (must be validated that it is a valid time);
//TODO: other time format could be used if it's easier/more effective to validate it;
//TODO: see the PHP manual/any tutorial for date/time validation functions;

/*
ALGORITHM:

1) Check that the sent phone_id is found in db; if not - ERROR: "invalid phone_id"

2) check that all necessary params (longitude, lattitude, data&time) ar not empty;

2a) check that such a message with these parameters is not already in the database (no-duplicates!!)

3) put the value in the database;
TODO: add addtiional fields in the database for server_time; store client time & server time also!

4) if everything ok - echo(0); else - some error;
TODO: we will need to implement a smart mechanism for reporting errors to the client application. By now just print it in plain text.

*/



$link = mysql_connect($mysql_host, $mysql_user, $mysql_password);
if (!$link) {
  die('Could not connect to database: ' . mysql_error());
}
//echo ("Connected to database successfully... <br/>");


$db = mysql_select_db($mysql_database, $link);
if (!$db) {
  mysql_error();
} else{
  //echo("Selected database successfully... <br/>"); 
}

// HERE add all the tasks that are necessary for interaction with the database

mysql_close($link);

//TEST: dummy echo 0;
echo(0);

?>
